<script>
	import store from '@/store';
	import {
		ACTIONS
	} from '@/store/modules/commonStore'
	
	import commonConfigs from "./setupproject/BASE_CONFIG"; // 项目基本配置
	
	import checkUpdate from '@/components/upgrade-center-app/utils/check-update'; // uni升级中心 - 客户端检查更新
	
	import { getInfoDeviceFun, getOAIDDeviceFun, setAsyncFunWait, setUrlToObjFun } from "@/setupproject/TOOLS_FUN";

	export default {
		async onLaunch (options) {
			console.log('onLaunch options', options);
			
			// #ifdef APP-PLUS
			plus.screen.lockOrientation("portrait-primary");
			// #endif
			
			// #ifdef APP-PLUS
			// uni升级中心 - 客户端检查更新
			checkUpdate();
			// #endif
			
			await store.commit('BASE_CONFIG_SET_FUN', commonConfigs);
			
			// 监听路径携带的参数，传给服务端
			if (options.query) {
				await store.commit('URL_QUERY_SET_FUN', options.query);
			}
			
			// 获取支付类型
			store.dispatch(ACTIONS.RECHARGE_TYPE_GET_FUN, {
				pack_name: this.$store.getters.commonConfigs.appPackageName,
				version: this.$store.getters.commonConfigs.appPackageVer,
				platform: uni.getSystemInfoSync().platform,
			});
			
			// #ifdef MP-WEIXIN
			// 微信小程序自动登陆,如有scene携带invite_uid等参数则在onShow执行业务
			if (!options.query.scene) {
				await store.commit('LOGINING_SET_FUN', true);
				await store.dispatch('LOGIN_GET_FUN', {loginType: 'mp'});
				await setAsyncFunWait(300);
				await store.commit('LOGINING_SET_FUN', false);
			}
			// #endif
			
			// #ifdef APP-PLUS
			// 设备信息： oaid (android 10+)
			let commonDeviceOaid = await getOAIDDeviceFun();
			// 设备信息： imsi, imei, uuid, idfa
			let commonDeviceInfo = await getInfoDeviceFun();
			await this.$store.commit('DEVICE_INFO_SET_FUN', {
				...commonDeviceOaid,
				...commonDeviceInfo,
			})
			
			// uni升级中心 - 客户端检查更新
			checkUpdate();
			
			// APP获取第三方配置
			store.dispatch(ACTIONS.THIRD_PARTY_GET_FUN, {
				pack_name: this.$store.getters.commonConfigs.appPackageName,
				version: this.$store.getters.commonConfigs.appPackageVer,
				platform: uni.getSystemInfoSync().platform,
			})
			// #endif
		},
		async onShow(options) {
			console.log('onShow options', options);
			
			// #ifdef MP-WEIXIN
			// 邀新功能
			if (options.query && options.query.scene || options.query.welfare_tag) {
				// scene 小程序二维码参数存放位置：{query: {scene: "invite_uid%3DASDA%3D"}}
				let scene = decodeURIComponent(options.query.scene);
				// 小程序分享
				let welfare_tag = decodeURIComponent(options.query.welfare_tag);
				if (scene || welfare_tag) {
					let obj = setUrlToObjFun(scene);
					let uid = obj.invite_uid; // // 邀请人uid
					if (uid || welfare_tag) {
						await store.commit('LOGINING_SET_FUN', true);
						await store.dispatch('LOGIN_GET_FUN', { invite_uid: uid || '', loginType: 'mp', welfare_tag: welfare_tag || '' });
						await setAsyncFunWait(300);
						await store.commit('LOGINING_SET_FUN', false);
					}
				}
			}
			
			// 小程序版本更新提醒
			const updateManager = uni.getUpdateManager();
			updateManager.onCheckForUpdate(function (result) {
				console.log('onCheckForUpdate:', result);
			});
			updateManager.onUpdateReady(function (result) {
				uni.showModal({
					title: '更新提示',
					content: '新版本已经准备好,是否重启应用?',
					showCancel: false,
					success(result) {
						if (result.confirm) {
							updateManager.applyUpdate();
						}
					}
				});
			});
			updateManager.onUpdateFailed(function (result) {
				console.log('onUpdateFailed:', result);
			});
			// #endif
		},
		onHide () {
		
		},
		methods: {
			
		}
	};
</script>

<style lang="scss">
	@import '@/static/css/font-style.scss';
	@import '@/static/css/flex-style.scss';
	@import '@/static/css/public-style.scss';
	@import '@/static/css/animate-style.scss';
</style>
